package cn.pli.bike.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;

import com.framemodule.base.BaseActivityPresenter;
import com.framemodule.base.netstatus.NetUtils;
import com.framemodule.utils.ImageResizer;

import java.io.File;

import cn.pli.bike.R;
import cn.pli.bike.bean.HttpResult;
import cn.pli.bike.bean.UserBean;
import cn.pli.bike.model.UserModel;
import cn.pli.bike.model.subscribers.ProgramSubscriber;
import cn.pli.bike.utils.AppUtils;
import cn.pli.bike.utils.ImageUtils;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class UserInfoDetailActivity extends BaseActivityPresenter<UserInfoDetailDelegate> implements View.OnClickListener {
    private UserBean userBean;


    @Override
    protected void initModel() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        userBean = (UserBean) getIntent().getSerializableExtra("userBean");
        if (userBean != null) {
            viewDelegate.updateUserInfo(userBean);
        }
        // viewDelegate.setData();




    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        viewDelegate.setOnClickListener(this, R.id.iv_title_back, R.id.ll_mine_short_video);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.iv_title_back:
                finish();
                break;
            //上传用户头像
            case R.id.ll_mine_short_video:
                ImageUtils.getInstance().selectPicture(this);
                break;
        }


    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (null == data) {
            return;
        }
        Uri uri = null;
        if (requestCode == ImageUtils.KITKAT_LESS) {
            uri = data.getData();
            // 调用裁剪方法
            ImageUtils.getInstance().cropPicture(this, uri);
        } else if (requestCode == ImageUtils.KITKAT_ABOVE) {
            uri = data.getData();
            // 先将这个uri转换为path，然后再转换为uri
            String thePath = ImageUtils.getInstance().getPath(this, uri);
            ImageUtils.getInstance().cropPicture(this,
                    Uri.fromFile(new File(thePath)));
        } else if (requestCode == ImageUtils.INTENT_CROP) {
            final Bitmap bitmap = data.getParcelableExtra("data");
            viewDelegate.getIvUserphoto().setImageBitmap(bitmap);


            Observable<File> observable = Observable.create(new ObservableOnSubscribe() {
                @Override
                public void subscribe(ObservableEmitter emitter) throws Exception {
                    File file = ImageResizer.saveBitmapFile(bitmap);
                    emitter.onNext(file);
                }
            });

            Consumer<File> consumer = new Consumer<File>() {
                @Override
                public void accept(File file) throws Exception {
                    UserModel.uploadUserPhoto(file.getAbsolutePath(), new ProgramSubscriber<HttpResult<String>>() {
                        @Override
                        public void onNext(HttpResult<String> stringHttpResult) {
                            AppUtils.savePhoto(stringHttpResult.getData());
                        }
                    }, null);
                }
            };
            observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(consumer);



          /*  File temp = new File(Environment.getExternalStorageDirectory()
                    .getPath() + "/yourAppCacheFolder/");// 自已缓存文件夹
            if (!temp.exists()) {
                temp.mkdir();
            }
            File tempFile = new File(temp.getAbsolutePath() + "/"
                    + Calendar.getInstance().getTimeInMillis() + ".jpg"); // 以时间秒为文件名
            // 图像保存到文件中
            FileOutputStream foutput = null;
            try {
                foutput = new FileOutputStream(tempFile);
                if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, foutput)) {
                    Toast.makeText(this,
                            "已生成缓存文件，等待上传！文件位置：" + tempFile.getAbsolutePath(),
                            Toast.LENGTH_LONG).show();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }*/
        }
    }


}
